﻿<UserControl x:Class="PeteEvans.AutoCompleteComboBox.AutoCompletionComboBox"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:autoCompleteComboBox="clr-namespace:PeteEvans.AutoCompleteComboBox"
             mc:Ignorable="d"
             d:DesignHeight="50" d:DesignWidth="200"
             FocusManager.IsFocusScope="True">
    <Grid x:Name="MainGrid" SnapsToDevicePixels="True" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
          d:DataContext="{d:DesignInstance autoCompleteComboBox:AutoCompletionComboBox}"
          LostKeyboardFocus="MainGrid_LostKeyboardFocus">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <TextBox Name="EditableTextBox" Grid.Row="0"
                 Margin="5"
                 HorizontalContentAlignment="Stretch"
                 VerticalContentAlignment="Stretch"
                 AllowDrop="False"
                 Height="25" Width="70"
                 AcceptsReturn="False"
                 AcceptsTab="False"
                 GotKeyboardFocus="EditableTextBox_GotKeyboardFocus"
                 PreviewMouseDown="EditableTextBox_PreviewMouseDown" />
        <Popup Name="popup" PlacementTarget="{Binding ElementName=EditableTextBox}" Placement="Bottom"
               IsOpen="{Binding IsDropDownOpen}" Height="Auto" Width="Auto">

            <Border Name="DropDownBorder"
                    BorderBrush="{DynamicResource {x:Static SystemColors.WindowFrameBrushKey}}"
                    BorderThickness="1"
                    Background="{DynamicResource {x:Static SystemColors.WindowBrushKey}}">
                <ListBox Name="ItemsPresenter"
                         KeyboardNavigation.DirectionalNavigation="Contained"
                         ItemsSource="{Binding SourceItems}" SelectionChanged="ItemsPresenter_SelectionChanged">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <TextBlock>
                                <Run Text="{Binding Prefix, Mode=OneWay}" />
                                <Run Text="{Binding Match, Mode=OneWay}" FontWeight="Bold" />
                                <Run Text="{Binding Suffix, Mode=OneWay}" />
                            </TextBlock>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
            </Border>
        </Popup>
    </Grid>
    <!-- NB. Need to expand focused element as an XML element
             to avoid issue with forward reference to the focused control -->
    <FocusManager.FocusedElement>
        <Binding ElementName="EditableTextBox" />
    </FocusManager.FocusedElement>
</UserControl>